package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.UsersPermission;
import com.jokerpan.project.service.PermissionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("permissions")
@Api("权限操作Api")
@PreAuthorize("hasPermission('','admin:list')")
public class PermissionsController {

    @Resource
    PermissionsService permissionsService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有权限分页")
    public ListResult<Object> queryList(UsersPermission usersPermission) {
        return ResultBuilder.buildListSuccess(this.permissionsService.queryList(usersPermission));
    }

    @GetMapping("/queryAll")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有权限数据")
    public ListResult<Object> queryAll() {
        return ResultBuilder.buildListSuccess(this.permissionsService.queryAll());
    }

    @PostMapping("/add")
    @ApiOperation("新增权限")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("权限管理,新增权限数据,新增权限,新增")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult addRole(@RequestBody UsersPermission usersPermission){
        if (this.permissionsService.add(usersPermission)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改权限")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("权限管理,修改权限数据,修改权限,修改")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult update(@RequestBody UsersPermission usersPermission){
        return this.permissionsService.update(usersPermission);
    }

    @GetMapping("/delete")
    @ApiOperation("删除权限")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("权限管理,删除权限数据,删除权限,删除")
    @PreAuthorize("hasPermission('','admin:delete')")
    public ActionResult delete(@RequestParam("id") Integer id){
        return this.permissionsService.deleteById(id);
    }
}
